package org.aplombh.java.awcing.basic.dp.knapsack;

import java.util.Scanner;

/**
 * 有 N 种物品和一个容量是 V 的背包，每种物品都有无限件可用。
 * <p>
 * 第 i 种物品的体积是 vi，价值是 wi。
 * <p>
 * 求解将哪些物品装入背包，可使这些物品的总体积不超过背包容量，且总价值最大。
 * 输出最大价值。
 * <p>
 * 输入格式
 * 第一行两个整数，N，V，用空格隔开，分别表示物品种数和背包容积。
 * <p>
 * 接下来有 N 行，每行两个整数 vi,wi，用空格隔开，分别表示第 i 种物品的体积和价值。
 * <p>
 * 输出格式
 * 输出一个整数，表示最大价值。
 * <p>
 * 数据范围
 * 0<N,V≤1000
 * 0<vi,wi≤1000
 * 输入样例
 * 4 10
 * 2 1
 * 2 4
 * 3 4
 * 4 5
 * 输出样例：
 * 10
 */
public class CompleteKnapsackProblem_3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        CompleteKnapsackProblem completeKnapsackProblem = new CompleteKnapsackProblem(n, m);
//        CompleteKnapsackProblemOptimize completeKnapsackProblem = new CompleteKnapsackProblemOptimize(n, m);
        for (int i = 1; i <= n; i++) {
            completeKnapsackProblem.v[i] = scanner.nextInt();
            completeKnapsackProblem.w[i] = scanner.nextInt();
        }
//        System.out.println(completeKnapsackProblem.solve());
        System.out.println(completeKnapsackProblem.solve_path());
    }
}

/**
 * 1. 0-1背包:  f[i][j] = max(f[i - 1][j], f[i - 1][j - v] + w);
 * 2. 完全背包:  f[i][j] = max(f[i - 1][j], f[i][j - v] + w);
 */

class CompleteKnapsackProblem {
    public static final int N = 1010;
    int n, m;
    int[][] f = new int[N][N];
    int[] v = new int[N];
    int[] w = new int[N];
    int[][] path = new int[N][N];

    public CompleteKnapsackProblem(int n, int m) {
        this.n = n;
        this.m = m;
    }

    public int solve() {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                f[i][j] = f[i - 1][j];
                if (j >= v[i]) {
                    f[i][j] = Math.max(f[i][j], f[i][j - v[i]] + w[i]);
                    // f[i][j] = max(f[i][j],f[i-1][j-v[i]],f[i-1][j-2v[i]],....)
                }
            }
        }
        return f[n][m];
    }

    public int solve_path() {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                f[i][j] = f[i - 1][j];
                if (j >= v[i]) {
                    if (f[i][j] < f[i][j - v[i]] + w[i]) {
                        f[i][j] = f[i][j - v[i]] + w[i];
                        path[i][j] = 1;
                    }
//                    f[i][j] = Math.max(f[i][j], f[i][j - v[i]] + w[i]);
                }
            }
        }
        path();
        return f[n][m];
    }

    public void path() {
        for (int i = n, j = m; i > 0 && j > 0; ) {
            if (path[i][j] == 1) {
                System.out.println(i);
                j -= v[i];
            } else {
                i--;
            }
        }
    }
}

class CompleteKnapsackProblemOptimize {
    public static final int N = 1010;
    int n, m;
    int[] f = new int[N];
    int[] v = new int[N];
    int[] w = new int[N];

    public CompleteKnapsackProblemOptimize(int n, int m) {
        this.n = n;
        this.m = m;
    }

    public int solve() {
        for (int i = 1; i <= n; i++) {
//            for (int j = m; j >= v[i]; j--) {
            for (int j = v[i]; j <= m; j++) {
                f[j] = Math.max(f[j], f[j - v[i]] + w[i]);
            }
        }
        return f[m];
    }
}
